package com.yerbamateprimer.round.state.exit.impl;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.yerbamateprimer.action.system.SystemAction;
import com.yerbamateprimer.round.state.State;
import com.yerbamateprimer.round.state.StateName;
import com.yerbamateprimer.round.state.StateStatus;
import com.yerbamateprimer.round.state.exit.StateExitCause;

/**
 * This class is useful to
 * model the behavior of the
 * state exit because a user
 * say "done" to a round pause.
 * 
 * @author Matias Liwski
 * @version 1.0
 */
public class PauseDoneStateExit extends StateExitTemplate {
	private StateName nextStateName; 
	private Multimap<Long, SystemAction> actions;

	/**
	 * Default constructor.
	 */
	public PauseDoneStateExit() {
		this.actions = HashMultimap.create();
	}

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.round.state.exit.StateExit#getCause()
	 */
	@Override
	public StateExitCause getCause() {
		return StateExitCause.PAUSE_DONE;
	}

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.round.state.exit.StateExit#getNextStateName()
	 */
	@Override
	public StateName getNextStateName() {
		return this.nextStateName;
	}

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.round.state.exit.impl.StateExitTemplate#setNextStateName(com.yerbamateprimer.round.state.StateName)
	 */
	@Override
	public void setNextStateName(StateName nextStateName) {
		Preconditions.checkArgument(nextStateName != null, "Couldn't set a null next state name.");
		
		this.nextStateName = nextStateName;
	}
	
	/* (non-Javadoc)
	 * @see com.yerbamateprimer.round.state.exit.StateExit#isThisExit(com.yerbamateprimer.round.state.State)
	 */
	@Override
	public boolean isThisExit(State state) {
		Preconditions.checkArgument(state != null, "Couldn't evaluate this exit without a state.");
		Preconditions.checkState(StateName.PAUSE.equals(state.getName()), "This exit could only be evaluated at PAUSE state (state: " + state + ").");
		
		return StateStatus.INTERRUPTED.equals(state.getStatus());
	}

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.round.state.exit.impl.StateExitTemplate#getActionsMap()
	 */
	@Override
	protected Multimap<Long, SystemAction> getActionsMap() {
		return this.actions;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE, false, true);
	}
}